set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The Robotics Library is a self-contained C++ library for robot kinematics, visualization, motion planning, and control.")

if(CMAKE_SIZEOF_VOID_P EQUAL 4)
	set(ARCHITECTURE "x86")
else()
	set(ARCHITECTURE "x64")
endif()

if(MSVC)
	if(CMAKE_CXX_COMPILER_VERSION MATCHES "^([0-9]+)\\.([0-9])([0-9]*)")
		if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 18)
			math(EXPR LINKER_VERSION_MAJOR "${CMAKE_MATCH_1} - 5")
		else()
			math(EXPR LINKER_VERSION_MAJOR "${CMAKE_MATCH_1} - 6")
		endif()
		set(PLATFORM_TOOLSET_MAJOR ${LINKER_VERSION_MAJOR})
		set(PLATFORM_TOOLSET_MINOR ${CMAKE_MATCH_2})
		set(COMPILER_VERSION "${PLATFORM_TOOLSET_MAJOR}.${PLATFORM_TOOLSET_MINOR}")
	endif()
	set(COMPILER_ID ${CMAKE_CXX_COMPILER_ID})
	set(CPACK_PACKAGE_FILE_NAME "rl-${PROJECT_VERSION}-${COMPILER_ID}-${COMPILER_VERSION}-${ARCHITECTURE}")
else()
	if(CMAKE_CXX_COMPILER_VERSION MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)")
		set(COMPILER_VERSION_MAJOR ${CMAKE_MATCH_1})
		set(COMPILER_VERSION_MINOR ${CMAKE_MATCH_2})
		set(COMPILER_VERSION_PATCH ${CMAKE_MATCH_3})
		set(COMPILER_VERSION "${COMPILER_VERSION_MAJOR}.${COMPILER_VERSION_MINOR}.${COMPILER_VERSION_PATCH}")
	else()
		set(COMPILER_VERSION ${CMAKE_CXX_COMPILER_VERSION})
	endif()
	if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
		set(COMPILER_ID "GCC")
	else()
		set(COMPILER_ID ${CMAKE_CXX_COMPILER_ID})
	endif()
	set(CPACK_PACKAGE_FILE_NAME "rl-${PROJECT_VERSION}-${CMAKE_SYSTEM_NAME}-${COMPILER_ID}-${COMPILER_VERSION}-${ARCHITECTURE}")
endif()

string(TOLOWER ${CPACK_PACKAGE_FILE_NAME} CPACK_PACKAGE_FILE_NAME)

if(WIN32)
	set(CPACK_PACKAGE_INSTALL_DIRECTORY "Robotics Library\\\\${PROJECT_VERSION}\\\\${COMPILER_ID}\\\\${COMPILER_VERSION}\\\\${ARCHITECTURE}")
else()
	set(CPACK_PACKAGE_INSTALL_DIRECTORY "rl-${PROJECT_VERSION}")
endif()

set(CPACK_PACKAGE_NAME "Robotics Library ${PROJECT_VERSION} - ${COMPILER_ID} ${COMPILER_VERSION} (${ARCHITECTURE})")

set(CPACK_COMPONENT_DEMOS_DEPENDS examples)
set(CPACK_COMPONENT_DEMOS_DESCRIPTION "Demo programs for the Robotics Library.")
set(CPACK_COMPONENT_DEMOS_DISPLAY_NAME "Demo Programs")
set(CPACK_COMPONENT_DEVELOPMENT_DESCRIPTION "Development files for the Robotics Library.")
set(CPACK_COMPONENT_DEVELOPMENT_DISPLAY_NAME "Development Files")
set(CPACK_COMPONENT_DOCUMENTATION_DESCRIPTION "Documentation for the Robotics Library.")
set(CPACK_COMPONENT_DOCUMENTATION_DISPLAY_NAME "Documentation")
set(CPACK_COMPONENT_EXAMPLES_DESCRIPTION "Example files for the Robotics Library.")
set(CPACK_COMPONENT_EXAMPLES_DISPLAY_NAME "Example Files")
set(CPACK_COMPONENT_EXTRAS_DESCRIPTION "Extra programs for the Robotics Library.")
set(CPACK_COMPONENT_EXTRAS_DISPLAY_NAME "Extra Programs")
set(CPACK_COMPONENT_RUNTIME_DESCRIPTION "Runtime files for the Robotics Library.")
set(CPACK_COMPONENT_RUNTIME_DISPLAY_NAME "Runtime Files")
set(CPACK_COMPONENT_SYSTEM_DESCRIPTION "Required system files for the Robotics Library.")
set(CPACK_COMPONENT_SYSTEM_DISPLAY_NAME "Required System Files")
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_DEMOS_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_DEMOS_PACKAGE_DEPENDS "librl-examples (= ${PROJECT_VERSION})")
set(CPACK_DEBIAN_DEMOS_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_DEVELOPMENT_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_DEVELOPMENT_PACKAGE_NAME "librl-dev")
set(CPACK_DEBIAN_DEVELOPMENT_PACKAGE_SECTION "libdevel")
set(CPACK_DEBIAN_DOCUMENTATION_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_DOCUMENTATION_PACKAGE_ARCHITECTURE "all")
set(CPACK_DEBIAN_DOCUMENTATION_PACKAGE_NAME "librl-doc")
set(CPACK_DEBIAN_DOCUMENTATION_PACKAGE_SECTION "doc")
set(CPACK_DEBIAN_EXAMPLES_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_EXAMPLES_PACKAGE_ARCHITECTURE "all")
set(CPACK_DEBIAN_EXTRAS_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_EXTRAS_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://www.roboticslibrary.org/")
set(CPACK_DEBIAN_PACKAGE_NAME "librl")
set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
set(CPACK_DEBIAN_PACKAGE_SOURCE "librl")
set(CPACK_DEBIAN_RUNTIME_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_RUNTIME_PACKAGE_NAME "librl")
set(CPACK_DEBIAN_RUNTIME_PACKAGE_RECOMMENDS "librl-examples (= ${PROJECT_VERSION})")
set(CPACK_DEBIAN_RUNTIME_PACKAGE_SECTION "libs")
set(CPACK_DEBIAN_RUNTIME_PACKAGE_SHLIBDEPS ON)
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
set(CPACK_NSIS_DISPLAY_NAME ${CPACK_PACKAGE_NAME})
set(CPACK_NSIS_EXECUTABLES_DIRECTORY ${CMAKE_INSTALL_BINDIR})
set(CPACK_NSIS_HELP_LINK "https://www.roboticslibrary.org/contact")
set(CPACK_NSIS_INSTALLED_ICON_NAME Uninstall.exe)
set(
	CPACK_NSIS_MENU_LINKS
	"https://www.roboticslibrary.org/" "RL"
	"https://github.com/roboticslibrary" "RL on GitHub"
	"https://launchpad.net/roblib" "RL on Launchpad"
	"https://sourceforge.net/projects/roblib" "RL on SourceForge"
	"https://www.youtube.com/roboticslibrary" "RL on YouTube"
)
set(CPACK_NSIS_MODIFY_PATH ON)
set(CPACK_NSIS_MUI_ICON ${CMAKE_CURRENT_SOURCE_DIR}/robotics-library.ico)
set(CPACK_NSIS_MUI_UNIICON ${CMAKE_CURRENT_SOURCE_DIR}/robotics-library.ico)
set(CPACK_NSIS_PACKAGE_NAME ${CPACK_PACKAGE_NAME})
set(CPACK_NSIS_URL_INFO_ABOUT "https://www.roboticslibrary.org/")
set(CPACK_PACKAGE_CONTACT "Robotics Library Team <roblib-developers@lists.sourceforge.net>")
set(CPACK_PACKAGE_VENDOR "Robotics Library")
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPackProjectConfig.cmake)
set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md)
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_RPM_DEMOS_FILE_NAME "RPM-DEFAULT")
set(CPACK_RPM_demos_PACKAGE_REQUIRES "rl-examples = ${PROJECT_VERSION}")
set(CPACK_RPM_DEVELOPMENT_FILE_NAME "RPM-DEFAULT")
set(CPACK_RPM_development_PACKAGE_NAME "rl-devel")
set(CPACK_RPM_DOCUMENTATION_FILE_NAME "RPM-DEFAULT")
set(CPACK_RPM_documentation_PACKAGE_GROUP "Documentation")
set(CPACK_RPM_EXAMPLES_FILE_NAME "RPM-DEFAULT")
set(
	CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
	/usr/lib/cmake
	/usr/lib64/cmake
	/usr/share/applications
	/usr/share/gtksourceview-3.0
	/usr/share/gtksourceview-3.0/language-specs
	/usr/share/icons
	/usr/share/icons/hicolor
	/usr/share/icons/hicolor/scalable
	/usr/share/icons/hicolor/scalable/apps
)
set(CPACK_RPM_EXTRAS_FILE_NAME "RPM-DEFAULT")
set(CPACK_RPM_PACKAGE_DESCRIPTION ${CPACK_PACKAGE_DESCRIPTION_SUMMARY})
set(CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
set(CPACK_RPM_PACKAGE_LICENSE "BSD")
set(CPACK_RPM_PACKAGE_NAME "rl")
set(CPACK_RPM_PACKAGE_SUMMARY "Robotics Library")
set(CPACK_RPM_PACKAGE_URL "https://www.roboticslibrary.org/contact")
set(CPACK_RPM_RUNTIME_FILE_NAME "RPM-DEFAULT")
set(CPACK_RPM_runtime_PACKAGE_NAME "rl")
set(CPACK_RPM_runtime_PACKAGE_SUGGESTS "rl-examples = ${PROJECT_VERSION}")
set(CPACK_WIX_COMPONENT_INSTALL OFF)
set(CPACK_WIX_EXTENSIONS "WiXUtilExtension")
set(
	CPACK_WIX_EXTRA_SOURCES
	${CMAKE_CURRENT_BINARY_DIR}/WixExtra.wxs
	${CMAKE_CURRENT_SOURCE_DIR}/cmake/WixUI_FeatureTreeNoLicense.wxs
)
set(CPACK_WIX_LICENSE_RTF ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md)
set(CPACK_WIX_PATCH_FILE ${CMAKE_CURRENT_BINARY_DIR}/WixPatch.xml)
set(CPACK_WIX_PRODUCT_ICON ${CMAKE_CURRENT_SOURCE_DIR}/robotics-library.ico)
set(CPACK_WIX_PROGRAM_MENU_FOLDER ${CPACK_PACKAGE_NAME})
set(CPACK_WIX_PROPERTY_ARPHELPLINK "https://www.roboticslibrary.org/contact")
set(CPACK_WIX_PROPERTY_ARPURLINFOABOUT "https://www.roboticslibrary.org/")
set(CPACK_WIX_ROOT_FEATURE_DESCRIPTION ${CPACK_PACKAGE_DESCRIPTION_SUMMARY})
set(CPACK_WIX_ROOT_FEATURE_TITLE "Robotics Library")
set(CPACK_WIX_UI_BANNER ${CMAKE_CURRENT_SOURCE_DIR}/cmake/WixUIBanner.bmp)
set(CPACK_WIX_UI_DIALOG ${CMAKE_CURRENT_SOURCE_DIR}/cmake/WixUIDialog.bmp)
set(CPACK_WIX_UI_REF "WixUI_FeatureTreeNoLicense")

get_cmake_property(COMPONENTS COMPONENTS)
list(FIND COMPONENTS demos DEMOS_COMPONENT_INDEX)
list(FIND COMPONENTS runtime RUNTIME_COMPONENT_INDEX)
list(FIND COMPONENTS system SYSTEM_COMPONENT_INDEX)

if(DEMOS_COMPONENT_INDEX GREATER -1)
	list(APPEND CPACK_DEBIAN_RUNTIME_PACKAGE_RECOMMENDS "librl-demos (= ${PROJECT_VERSION})")
	list(APPEND CPACK_RPM_runtime_PACKAGE_SUGGESTS "rl-demos = ${PROJECT_VERSION}")
endif()

if(RUNTIME_COMPONENT_INDEX GREATER -1)
	list(APPEND CPACK_COMPONENT_DEMOS_DEPENDS runtime)
	list(APPEND CPACK_COMPONENT_DEVELOPMENT_DEPENDS runtime)
	list(APPEND CPACK_DEBIAN_DEMOS_PACKAGE_DEPENDS "librl (= ${PROJECT_VERSION})")
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "librl (= ${PROJECT_VERSION})")
	list(APPEND CPACK_RPM_demos_PACKAGE_REQUIRES "rl = ${PROJECT_VERSION}")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "rl = ${PROJECT_VERSION}")
endif()

if(SYSTEM_COMPONENT_INDEX GREATER -1)
	list(APPEND CPACK_COMPONENT_DEMOS_DEPENDS system)
	list(APPEND CPACK_COMPONENT_DEVELOPMENT_DEPENDS system)
	list(APPEND CPACK_COMPONENT_EXTRAS_DEPENDS system)
	list(APPEND CPACK_COMPONENT_RUNTIME_DEPENDS system)
endif()

if(Boost_INCLUDE_DIRS)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libboost-dev (>= 1.46)")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "boost-devel >= 1.46")
endif()
if(BULLET_INCLUDE_DIR AND BULLET_BULLETCOLLISION_LIBRARY_RELEASE AND BULLET_BULLETDYNAMICS_LIBRARY_RELEASE AND BULLET_BULLETSOFTBODY_LIBRARY_RELEASE AND BULLET_LINEARMATH_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libbullet-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "bullet-devel")
endif()
if(Coin_INCLUDE_DIR AND Coin_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libcoin-dev | libcoin80-dev | libcoin60-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "Coin4-devel or Coin3-devel or Coin2-devel")
endif()
if(COMEDI_INCLUDE_DIR AND COMEDI_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libcomedi-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "comedilib-devel")
endif()
if(EIGEN3_INCLUDE_DIR)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libeigen3-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "eigen3-devel")
endif()
if(FCL_INCLUDE_DIR AND FCL_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libfcl-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "fcl-devel")
endif()
if(LIBDC1394_INCLUDE_DIR AND LIBDC1394_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libdc1394-dev | libdc1394-22-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "libdc1394-devel")
endif()
if(LIBXML2_INCLUDE_DIR AND LIBXML2_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libxml2-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "libxml2-devel")
endif()
if(LIBXSLT_INCLUDE_DIR AND LIBXSLT_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libxslt1-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "libxslt-devel")
endif()
if(NLOPT_INCLUDE_DIR AND NLOPT_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libnlopt-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "NLopt-devel")
endif()
if(ODE_INCLUDE_DIR AND ODE_LIBRARY_RELEASE)
	list(APPEND CPACK_DEBIAN_DEVELOPMENT_PACKAGE_DEPENDS "libode-dev")
	list(APPEND CPACK_RPM_development_PACKAGE_REQUIRES "ode-devel")
endif()

foreach(component IN LISTS COMPONENTS)
	string(TOUPPER ${component} COMPONENT)
	if(DEFINED CPACK_DEBIAN_${COMPONENT}_PACKAGE_DEPENDS)
		string(REPLACE ";" ", " CPACK_DEBIAN_${COMPONENT}_PACKAGE_DEPENDS "${CPACK_DEBIAN_${COMPONENT}_PACKAGE_DEPENDS}")
	endif()
	if(DEFINED CPACK_DEBIAN_${COMPONENT}_PACKAGE_RECOMMENDS)
		string(REPLACE ";" ", " CPACK_DEBIAN_${COMPONENT}_PACKAGE_RECOMMENDS "${CPACK_DEBIAN_${COMPONENT}_PACKAGE_RECOMMENDS}")
	endif()
	if(DEFINED CPACK_RPM_${component}_PACKAGE_REQUIRES)
		string(REPLACE ";" ", " CPACK_RPM_${component}_PACKAGE_REQUIRES "${CPACK_RPM_${component}_PACKAGE_REQUIRES}")
	endif()
	if(DEFINED CPACK_RPM_${component}_PACKAGE_SUGGESTS)
		string(REPLACE ";" ", " CPACK_RPM_${component}_PACKAGE_SUGGESTS "${CPACK_RPM_${component}_PACKAGE_SUGGESTS}")
	endif()
endforeach()

string(REPLACE ";" "\n" CPACK_NSIS_CREATE_ICONS_EXTRA "${CPACK_NSIS_CREATE_ICONS_EXTRA}")
string(REPLACE ";" "\n" CPACK_NSIS_DELETE_ICONS_EXTRA "${CPACK_NSIS_DELETE_ICONS_EXTRA}")
string(REPLACE "/>;<Shortcut" "/>\n<Shortcut" WIX_SHORTCUTS "${WIX_SHORTCUTS}")

configure_file(cmake/WixExtra.wxs.in ${CMAKE_CURRENT_BINARY_DIR}/WixExtra.wxs)
configure_file(cmake/WixPatch.xml.in ${CMAKE_CURRENT_BINARY_DIR}/WixPatch.xml)
